package com.koalac.ironman.weixin;

import android.content.Context;

import com.tencent.mm.sdk.modelmsg.SendAuth;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

public enum FootTravelWechat {
    
    INSTANCE;
    
    public static final String APP_ID = "wxec895d3e6ed92b55";
    public static final String APP_SECRET = "d4624c36b6795d1d99dcf0547af5443d";
    
    /**
	 * IWXAPI是第三方app和微信通信的openapi接口
	 */
	private static IWXAPI api = null;//单例
    
	
private Context oldContext = null;
	
	public IWXAPI getWXAPI(Context context){
		oldContext = context;
		if(api == null){
			regToWx(context);
		}
		return api;
	}
	
	public void regToWx(Context context){
		/**
		 * 如果api在当前的Activity初始化过一次,跳过创建的过程
		 */
		if(api != null && oldContext == context){
			return;
		}
		oldContext = context;
		/**
		 * 通过WXAPIFactory工厂,获取IWXAPI的实例
		 */
		api = WXAPIFactory.createWXAPI(context, null);
		/**
		 * 将应用的appId注册到微信
		 */
		api.registerApp(APP_ID);
	}
	
	/**
	 * 登陆 
	 */
	public void login() {
		final SendAuth.Req req = new SendAuth.Req();
		req.scope = "snsapi_userinfo";
		req.state = "wechat_sdk_demo_test";
		api.sendReq(req);
	}
}
